from flask import Flask, render_template, request,jsonify,escape
from panduan import panduan_login
from vsearch import search4letters
from pymysql import connect




def look_over():
	con = connect(host="127.0.0.1",port=3306,user="root",password="1234abcd",db="user")
	cursor1 = con.cursor()
	sql = "select * from student"
	cursor1.execute(sql)
	res = cursor1.fetchall()
	print(res)
	return res



app = Flask(__name__)


@app.route('/')
def hello() -> str:
	return render_template("index.html")



@app.route('/addlogin',methods=['POST'])
def addlogin() -> 'html':
	zhanghao = request.form.get("zhanghao") 
	mima = request.form.get("mima")
	a = panduan_login(zhanghao, mima)
	if a == "true":
		student_title = ["序号","图书分类","图书出版社","图书数量"]
		res=look_over()
		return render_template('entry.html',the_title ="登录成功，欢迎您进入图书管理系统！",student_title=student_title,student_table=res)
	else:
		return render_template('faillogin.html')

@app.route('/faillogin',methods=['POST','GET'])
def faillogin() -> 'html':
	return render_template('index.html')


@app.route('/entry')
def entry_page() -> 'html':
	student_title = ["序号","图书分类","图书出版社","图书数量"]
	res=look_over()
	return render_template('entry.html',the_title ="欢迎您进入图书管理系统！",student_title=student_title,student_table=res)

@app.route('/deletestudent', methods=["POST"])
def delete_student() -> 'html':
	con = connect(host="127.0.0.1",port=3306,user="root",password="1234abcd",db="user")
	cursor2 = con.cursor()
	student_id = request.form.get("deleteitem")
	sql = "delete from student where rewoid=%s"
	cursor2.execute(sql% student_id)
	con.commit()
	student_title = ["序号","图书分类","图书出版社","图书数量"]
	res=look_over()
	return render_template('entry.html',the_title ="欢迎来到图书管理系统!",student_title=student_title,student_table=res)

@app.route("/xiugaibook",methods=["POST"])
def modify_student():
	con = connect(host="127.0.0.1",port=3306,user="root",password="1234abcd",db="user")
	cursor3 = con.cursor()
	student_id = request.form.get("additem")
	sql = "select * from student where rewoid=%s"%student_id
	cursor3.execute(sql)
	res= cursor3.fetchall()
	student_name= res[0][1]
	student_gender=res[0][2]
	student_class=res[0][3]
	return render_template("xiugaistudent.html",student_id=student_id,student_name=student_name,student_gender=student_gender,student_class=student_class)


@app.route('/xiugai',methods=["POST"])
def modify_page() -> 'html':
	con = connect(host="127.0.0.1",port=3306,user="root",password="1234abcd",db="user")
	cursor4 = con.cursor()
	student_id = request.form.get("student_id")
	student_name = request.form.get("student_name")
	student_gender = request.form.get("student_gender")
	student_class = request.form.get("student_class")
	sql = "update student set name='%s',gender='%s',class='%s' where rewoid='%s'"% (student_name,student_gender,student_class,student_id)
	print(sql)
	cursor4.execute(sql)
	con.commit()
	return render_template("modifysecesspage.html")

@app.route('/addbook',methods=["GET"])
def addstudent_page() -> "html":
	return render_template("addstudentpage.html")


@app.route('/addstudent',methods=["POST"])
def add_student():
	con = connect(host="127.0.0.1",port=3306,user="root",password="1234abcd",db="user")
	cursor5 = con.cursor()
	student_id = request.form.get("student_id")
	student_name = request.form.get("student_name")
	student_gender = request.form.get("student_gender")
	student_class = request.form.get("student_class")
	sql = "insert into student set rewoid='%s', name='%s',gender='%s',class='%s'"% (student_id, student_name,student_gender,student_class)
	cursor5.execute(sql)
	con.commit()
	return render_template("addsucesspage.html")

@app.route('/findbook',methods=["POST"])
def find_book():
	con = connect(host="127.0.0.1",port=3306,user="root",password="1234abcd",db="user")
	cursor6 = con.cursor()
	student_gender = request.form.get("student_gender")
	print(student_gender)
	if student_gender!='' :
		sql = "select * from student where gender='%s'"% (student_gender)
	
	else:
		sql = "select * from student"
	cursor6.execute(sql)
	res = cursor6.fetchall()
	student_title = ["序号","图书分类","图书出版社","图书数量"]
	return render_template("entry.html",the_title ="登录成功，欢迎您进入学生管理信息系统！",student_title=student_title,student_table=res)





if __name__ == '__main__':
	app.run(debug = True)